package com.example.baserecyclerviewadapterdemo.adapter;

import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.entity.DelegateMultiEntity;
import com.example.baserecyclerviewadapterdemo.entity.QuickMultipleEntity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class DelegateMultiAdapter extends BaseDelegateMultiAdapter<DelegateMultiEntity, BaseViewHolder> {
    public DelegateMultiAdapter(@Nullable List<DelegateMultiEntity> data) {
        super(data);
        setMultiTypeDelegate(new MyMultiTypeDelegate());
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, DelegateMultiEntity delegateMultiEntity) {
        switch (holder.getItemViewType()) {
            case QuickMultipleEntity.TEXT:
                holder.setText(R.id.tv,"CymChad " + holder.getLayoutPosition());
                break;
            case QuickMultipleEntity.IMG_TEXT:
                switch (holder.getLayoutPosition() % 2) {
                    case 0:
                        holder.setImageResource(R.id.iv,R.mipmap.animation_img1);
                        break;
                    case 1:
                        holder.setImageResource(R.id.iv,R.mipmap.animation_img2);
                        break;
                }
                holder.setText(R.id.tv,"JohnConnor " + holder.getLayoutPosition());
                break;
            default:
                break;
        }
    }

    final static class MyMultiTypeDelegate extends BaseMultiTypeDelegate<DelegateMultiEntity> {

        public MyMultiTypeDelegate() {
            addItemType(DelegateMultiEntity.TEXT, R.layout.item_text_view);
            addItemType(DelegateMultiEntity.IMG, R.layout.item_image_view);
            addItemType(DelegateMultiEntity.IMG_TEXT, R.layout.item_img_text_view);
        }

        @Override
        public int getItemType(@NotNull List<? extends DelegateMultiEntity> data, int position) {
            switch (position % 3) {
                case 0:
                    return DelegateMultiEntity.TEXT;
                case 1:
                    return DelegateMultiEntity.IMG;
                case 2:
                    return DelegateMultiEntity.IMG_TEXT;
                default:
                    break;
            }
            return 0;
        }
    }
}
